// pages/order/refund/index.js

const customRequst = require('../../../common/customRequst');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    isSubmit: false,
    formToken: '',
    reason: '',
  },
  onLoad: function (option) {
    let that = this;
    if (option.id) {
      that.setData({
        id: option.id
      })
    }
  },
  onShow: function () {
    let that = this;
    if (that.data.id) {
      customRequst.postRequest('api/common/get_form_token', {}).then(res => {
        if (res.code == 1) {
          that.setData({
            formToken: res.data.token
          })
        } else {
          wx.showToast({
            title: res.msg,
          })
        }
      })
    }
  },
  bindFormSubmit: function (e) {
    let that = this
    let reason = e.detail.value.textarea;
    if (!reason) {
      wx.showToast({
        title: '退款原因不能为空',
        icon: 'error'
      })
      return;
    }
    if (!that.data.id) {
      return;
    }
    if (that.data.isSubmit) {
      wx.showToast({
        title: '请勿重复提交',
        icon: 'error'
      });
      return;
    }
    that.setData({
      isSubmit: true
    });
    customRequst.postRequest('api/contribution/refund', {
      id: that.data.id,
      reason: reason,
      token: that.data.formToken
    }).then(res => {
      if (res.code == 1) {
        wx.showToast({
          title: '申请退款成功，管理员正在处理中。。。',
          duration: 3000,
          icon: 'success',
          success: function () {
            let url = '/pages/order/list/index?type=4';
            wx.navigateTo({
              url: url
            })
          }
        })
      } else {
        that.setData({
          isSubmit: false,
          formToken: res.data.token
        });
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  }

})